package com.evil.application.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evil.account.api.RemoteUserEnterpriseService;
import com.evil.application.api.RemoteApplicationGroupService;
import com.evil.application.mapper.ApplicationGroupMapper;
import com.evil.application.mapper.ApplicationPackageMapper;
import com.evil.application.pojo.dto.group.AddGroupReqDTO;
import com.evil.application.pojo.dto.group.ModifyGroupReqDTO;
import com.evil.application.pojo.entity.ApplicationGroup;
import com.evil.application.pojo.entity.ApplicationPackage;
import com.evil.application.service.ApplicationGroupService;
import com.evil.application.service.PackageApplicationService;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.application.dto.pkg.ModifyPackageApplicationSourceDTO;
import com.evil.common.application.dto.pkg.PackageApplicationSourceDTO;
import com.evil.common.application.enums.ApplicationSourceTypeEnum;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.file.api.RemoteFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * 应用组表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ApplicationGroupServiceImpl extends ServiceImpl<ApplicationGroupMapper, ApplicationGroup> implements ApplicationGroupService, RemoteApplicationGroupService {

    private final LoginUtil loginUtil;

    private final ApplicationPackageMapper applicationPackageMapper;

    private final PackageApplicationService packageApplicationService;

    private final RemoteUserEnterpriseService remoteUserEnterpriseService;

    private final RemoteFileService remoteFileService;

    private final ApplicationLogServiceImpl applicationLogService;

    /**
     * 新增应用组
     *
     * @param addGroupReqDTO addGroupReqDTO
     */
    @Override
    public void addGroup(AddGroupReqDTO addGroupReqDTO) {
        Long loginEnterpriseId = loginUtil.getLoginEnterpriseId();
        // 检查是否是管理员
        remoteUserEnterpriseService.checkEnterpriseManager(loginUtil.getLoginUserEnterprise());

        Long applicationPackageId = addGroupReqDTO.getPackageId();
        ApplicationPackage applicationPackage = applicationPackageMapper.findById(applicationPackageId);
        // 检查企业
        loginUtil.checkEnterpriseId(applicationPackage.getEnterpriseId());

        ApplicationGroup applicationGroup = new ApplicationGroup();
        applicationGroup.setEnterpriseId(loginEnterpriseId);
        applicationGroup.setGroupName(addGroupReqDTO.getGroupName());
        // icon
        if (StringUtils.isNotBlank(addGroupReqDTO.getIconFileCode())) {
            FileRespDTO iconFileBase = remoteFileService.findBaseByFileCode(addGroupReqDTO.getIconFileCode());
            applicationGroup.setIcon(iconFileBase.getFileId());
        }
        applicationGroup.setIsEnable(SwitchEnum.YES.getId());
        applicationGroup.setIsDeleted(SwitchEnum.NO.getId());

        applicationLogService.saveAndCheck(this.baseMapper, ApplicationGroup::getApplicationGroupId, applicationGroup);

        // 应用包应用表
        ModifyPackageApplicationSourceDTO modifyPackageApplicationReqDTO = new ModifyPackageApplicationSourceDTO();
        modifyPackageApplicationReqDTO.setSourceType(ApplicationSourceTypeEnum.GROUP.getId());
        modifyPackageApplicationReqDTO.setSourceId(applicationGroup.getApplicationGroupId());
        modifyPackageApplicationReqDTO.setPackageIds(Collections.singletonList(applicationPackageId));
        packageApplicationService.savePackageApplications(modifyPackageApplicationReqDTO);
    }

    /**
     * 编辑应用组
     *
     * @param modifyGroupReqDTO modifyGroupReqDTO
     */
    @Override
    public void modifyGroup(ModifyGroupReqDTO modifyGroupReqDTO) {
        // 检查是否是管理员
        remoteUserEnterpriseService.checkEnterpriseManager(loginUtil.getLoginUserEnterprise());

        ApplicationGroup applicationGroup = this.baseMapper.findById(modifyGroupReqDTO.getApplicationGroupId());
        // 检查企业
        loginUtil.checkEnterpriseId(applicationGroup.getEnterpriseId());

        applicationGroup.setGroupName(modifyGroupReqDTO.getGroupName());

        applicationLogService.saveAndCheck(this.baseMapper, ApplicationGroup::getApplicationGroupId, applicationGroup);
    }

    /**
     * 删除应用组
     *
     * @param applicationGroupId applicationGroupId
     */
    @Override
    public void deleteGroup(Long applicationGroupId) {
        ApplicationGroup applicationGroup = this.baseMapper.findById(applicationGroupId);
        // 检查企业
        loginUtil.checkEnterpriseId(applicationGroup.getEnterpriseId());
        // 检查是否是管理员
        remoteUserEnterpriseService.checkEnterpriseManager(loginUtil.getLoginUserEnterprise());

        applicationGroup.setIsDeleted(SwitchEnum.YES.getId());
        applicationLogService.save(this.baseMapper, ApplicationGroup::getApplicationGroupId, applicationGroup);

        // 删除应用包应用
        packageApplicationService.deletePackageApplication(new PackageApplicationSourceDTO(
                ApplicationSourceTypeEnum.GROUP, applicationGroupId));
    }
}
